package com.zlsy.springbootdemo.mt.impl;

import com.zlsy.springbootdemo.mt.dao.MtUserModule;
import com.zlsy.springbootdemo.mt.dao.UserDao;
import com.zlsy.springbootdemo.mt.enums.UserEnums;
import com.zlsy.springbootdemo.mt.reward.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhouliang
 * @date 2021/1/22 15:31
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class InviteRewardImpl {
  private final UserDao userDao;
  private final FactorRewardStrategyFactory factorRewardStrategyFactory;

  public void sendReward(Long userId) {
    FactorRewardStrategyFactory strategyFactory = new FactorRewardStrategyFactory();
    MtUserModule userModule = userDao.getInviteeByUserId(userId);
    if (userModule.getType() == 1) {
      RewardStrategy strategy = strategyFactory.createStrategy(NewUserRewardStrategyA.class);
      RewardContext rewardContext = new RewardContext(strategy);
      rewardContext.doStrategy(userId);
    }

    if (userModule.getType() == 2) {
      RewardStrategy strategy = strategyFactory.createStrategy(OldUserRewardStrategyA.class);
      RewardContext rewardContext = new RewardContext(strategy);
      rewardContext.doStrategy(userId);
    }
  }

  public void sendRewardUp(Long userId) {
    MtUserModule userModule = userDao.getInviteeByUserId(userId);
    UserEnums instance = UserEnums.getInstance(userModule.getType());
    RewardStrategy strategy = factorRewardStrategyFactory.createStrategy(instance);
    RewardContext context = new RewardContext(strategy);
    context.doStrategy(userId);
  }
}
